//
//  HDCenterTableHeader.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/4/7.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

protocol HDCenterTableHeaderDelegate :class {
    func didEditUserInfo()
}

class HDCenterTableHeader: UIView {
    
    weak var delegate:HDCenterTableHeaderDelegate?
    let backImageView = UIImageView()
    let headImageView = UIButton(type: .custom)
    let nameLabel = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupSubViews()
    }
    
    func setupSubViews() {
        
        
        backImageView.frame = self.bounds
        backImageView.isUserInteractionEnabled = true
        backImageView.image = UIImage(named: "user_icon")
        self.addSubview(backImageView)
        
        let headImgWH:CGFloat = 100
        let topBorder:CGFloat = (backImageView.frame.size.height - headImgWH)/2 - 20
        headImageView.frame = CGRect(x: (self.frame.size.width - headImgWH)/2, y: topBorder, width: headImgWH, height: headImgWH)
        headImageView.layer.cornerRadius = headImageView.frame.size.width / 2
        headImageView.clipsToBounds = true
        headImageView.setBackgroundImage(UIImage(named: "个人中心默认头像"), for: UIControlState())
        headImageView.layer.borderColor = UIColor(hex:0xfbd080).cgColor
        headImageView.layer.borderWidth = 2
        headImageView.adjustsImageWhenHighlighted = false
        headImageView.addTarget(self, action: #selector(HDCenterTableHeader.editUserInfo), for: .touchUpInside)
        backImageView.addSubview(headImageView)
        
        
        nameLabel.frame = CGRect(x: 0, y: headImageView.frame.origin.y + headImageView.frame.size.width + 20, width: self.frame.size.width, height: 16)
        nameLabel.titleLabel?.textAlignment = .center
        nameLabel.setTitleColor(UIColor.white, for: UIControlState())
        nameLabel.setTitle("注册/登录", for: UIControlState())
        
        backImageView.addSubview(nameLabel)
        nameLabel.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        nameLabel.addTarget(self, action: #selector(HDCenterTableHeader.editUserInfo), for: .touchUpInside)
    }
    
    func editUserInfo() {
        delegate?.didEditUserInfo()
    }
    
    func refreshHeaderView( _ name:String) {
        nameLabel.setTitle(name, for: UIControlState())
        
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
